package com.qxzn.module.home.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.AuthInfoBean;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.widget.ToastUtil;
import com.qxzn.app.page.widget.statusbar.StatusBar;
import com.qxzn.app.util.CommonUtil;
import com.qxzn.module.home.R;
import com.qxzn.module.home.R2;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RespondJoinFamilyActivity extends BaseActivity {
    @BindView(R2.id.iv_head)
    ImageView ivHead;
    @BindView(R2.id.tv_name)
    TextView tvName;
    @BindView(R2.id.tv_admin)
    TextView tvAdmin;
    @BindView(R2.id.tv_id)
    TextView tvId;
    @BindView(R2.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R2.id.tv_notice)
    TextView tvNotice;

    AuthInfoBean authInfoBean = null;

    @Override
    protected int initLayout() {
        return R.layout.activity_respond_join_family;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void iniTitleBar() {
        title_bar.setVisibility(View.GONE);
    }

    @Override
    protected void setStatusBarColor() {
        StatusBar.setStatusBarLightMode(this, getResources().getColor(R.color.blue_3FC7FF));
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        authInfoBean = (AuthInfoBean) getIntent().getSerializableExtra("AuthInfoBean");
        if (authInfoBean == null) {
            ToastUtil.showTextToast(this, "数据有误");
            finish();
            return;
        }
        tvName.setText(authInfoBean.robot_name);
        tvAdmin.setText(authInfoBean.admin_name);
        tvId.setText("ID:" + authInfoBean.robot_id);
    }


    @OnClick({R2.id.iv_back1, R2.id.tv_confirm})
    public void onViewClicked(View view) {
        if (CommonUtil.isFastClick()) {
            return;
        }
        int i = view.getId();
        if (i == R.id.iv_back1) {
            finish();
        } else if (i == R.id.tv_confirm) {
            showProgress();
            JSONObject params = new JSONObject();
            params.put("device_id", authInfoBean.robot_id);
            params.put("username", authInfoBean.user_id);
            params.put("roles", "1");
            HttpRequest.getService().agreeInvitation(HttpRequest.getDefaultBody(params)).enqueue(new Callback<BaseBean>() {
                @Override
                public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                    hideProgress();
                    BaseBean baseBean = response.body();
                    if (baseBean != null) {
                        if (baseBean.getCode() == 200) {
                            ToastUtil.showImgToast(RespondJoinFamilyActivity.this, "已同意", R.mipmap.icon_success);
                            finish();
                            return;
                        }
                    }
                    if (baseBean != null && !TextUtils.isEmpty(baseBean.getMsg())) {
                        ToastUtil.showImgToast(RespondJoinFamilyActivity.this, baseBean.getMsg(), R.mipmap.icon_failed);
                    } else {
                        ToastUtil.showImgToast(RespondJoinFamilyActivity.this, "加入失败，请重试", R.mipmap.icon_failed);
                    }
                }

                @Override
                public void onFailure(Call<BaseBean> call, Throwable t) {
                    hideProgress();
                    ToastUtil.showTextToast(RespondJoinFamilyActivity.this, getString(R.string.network_error));
                }
            });
        }
    }


}
